package cn.edu.swu.online;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;

public class onlineListener implements HttpSessionListener, ServletContextListener {
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext servletContext=se.getSession().getServletContext();
        int online=(int)servletContext.getAttribute("online");
        online++;
        servletContext.setAttribute("online",online);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext servletContext=se.getSession().getServletContext();
        int online=(int)servletContext.getAttribute("online");
        online--;
        servletContext.setAttribute("online",online);
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext sc=sce.getServletContext();
        sc.setAttribute("online",0);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) { }
}


